﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using IT_Tracker.ENTIDADES;
using IT_Tracker.DADOS;

using ComponentFactory.Krypton.Toolkit;

namespace IT_Tracker
{
    public partial class Form_TicketAtualiza : KryptonForm
    {
        public Form_TicketAtualiza(string forn, Usuario sUser, Ticket TktAlt)
        {
            InitializeComponent();

            fornecedor = forn;
            sessionUser = sUser;
            tkt = TktAlt;
        }

        private void Form_TicketAtualiza_Load(object sender, EventArgs e)
        {
            cboStatus.Text = tkt.Status;
            txtTicket.Text = Convert.ToString(tkt.ticket);
            txtDescricao.Text = tkt.Descricao;

            if (Convert.ToString(tkt.DataConclusao) != "01/01/1900 00:00:00")
            {
                mtbEncerramento.Text = Convert.ToString(tkt.DataConclusao);
            }
            if (Convert.ToString(tkt.DataPrevisao) != "01/01/1900 00:00:00")
            {
                mtbPrevisao.Text = Convert.ToString(tkt.DataPrevisao);
            }

        }


        #region Declarações e Variavéis
        string fornecedor;

        //Entidades/Classes
        Ticket tkt = new Ticket();
        Usuario sessionUser = new Usuario();
        ArrayList comboTipo = new ArrayList();
        ArrayList comboOper = new ArrayList();

        //Metodos BD
        PersistenciaBDTicket bdTkt = new PersistenciaBDTicket();

        #endregion

        #region *************Botões TextBOX
        private void btnTxtPrevisao_Click(object sender, EventArgs e)
        {
            mtbPrevisao.Clear();

        }

        private void btnTxtEncerramento_Click(object sender, EventArgs e)
        {
            mtbEncerramento.Clear();
        }

        private void btntxtStatusEsq_Click(object sender, EventArgs e)
        {
            if ((cboStatus.SelectedIndex) >= 1)
            {
                cboStatus.SelectedIndex--;
            }

        }

        private void btntxtStatusDir_Click(object sender, EventArgs e)
        {
            if ((cboStatus.Items.Count - 1) > cboStatus.SelectedIndex)
            {
                cboStatus.SelectedIndex++;
            }

        }
        #endregion

        private void btnAtualizar_Click(object sender, EventArgs e)
        {
            if ((mtbEncerramento.Text == "  /  /       :") && (cboStatus.Text == "Encerrado") | (cboStatus.Text == "Sem Intervenção"))
            {
                MessageBox.Show("** " + DateTime.Now.ToString() + " ** Error -> Status: Encerrado - Favor inserir data de encerramento");
            }
            
            else
            {
                tkt.Status = cboStatus.Text;

                if (mtbEncerramento.Text != "  /  /       :")
                {
                    tkt.DataConclusao = Convert.ToDateTime(mtbEncerramento.Text);
                }
                else { tkt.DataConclusao = tkt.DataConclusao; }

                if (mtbPrevisao.Text != "  /  /")
                {
                    tkt.DataPrevisao = Convert.ToDateTime(mtbPrevisao.Text);
                }
                else { tkt.DataPrevisao = tkt.DataPrevisao; }

                tkt.DataAtualizacao = DateTime.Now;
                tkt.Usuario = sessionUser.Nome;

                tkt.Resumo = ("\r ====Atualizado Por: " + Convert.ToString(sessionUser.Nome) + " as " + DateTime.Now + "==== \r " + rtxtResumo.Text) + "\r " + tkt.Resumo;

                try
                {
                    bdTkt.atualizarTicket(tkt, "spTicketAtualiza");
                    Form_Ticket.retorno = ("** " + DateTime.Now.ToString() + " ** Ticket #" + tkt.ticket + " atualizado com sucesso!");
                    Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("** " + DateTime.Now.ToString() + " ** Error -> " + ex.Message);

                }
            }
        }





    }
}
